package com.pulsarwebdevelopment.concordance;

import java.util.ArrayList;
import java.util.List;

public abstract class ConcordanceOutput<K, V> {

	/**
	 * The list of values that is currently being held by this object.
	 */
	protected List<V> curValues = new ArrayList<V>();
	
	/**
	 * The current key that is being used by this object.
	 */
	protected K curKey;
	
	/**
	 * Add another value to this key. If this key does not match the previous key, then flush the output.
	 * @param key The key to which to add a value.
	 * @param value The value to add.
	 */
	public void addValueToKey(K key, V value){
		if(value == null || key == null){
			return; //should this throw an exception?
		}

		if(key.equals(curKey)){
			curValues.add(value);
		}else{
			this.flush();
			this.curKey = key;
			this.curValues = new ArrayList<V>();
			this.curValues.add(value);
		}

	}

	/**
	 * Write the current list of values with the current key to whatever backing object is being used.
	 */
	public abstract void flush();

}